<?php
/**
 * BomsController.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-13 23:12:45
 * @modified    2023-02-13 23:12:45
 *
 */

namespace App\Http\Controllers;

use App\Models\Product\Bom;
use App\Repositories\Product\BomRepo;
use Illuminate\Http\Request;

class BomsController extends BaseController
{
    /**
     * @OA\Get(
     *     path="/boms",
     *     tags={"Bom"},
     *     summary="获取配件列表",
     *     description="获取配件列表",
     *     operationId="bom_index",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="name", in="query", description="配件名称", required=false, @OA\Schema(default="", type="string")),
     *     @OA\Parameter(name="status", in="query", description="状态", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="page", in="query", description="当前页数", required=false, @OA\Schema(default="", type="integer")),
     *     @OA\Parameter(name="limit", in="query", description="每页数量(默认10)", required=false, @OA\Schema(default=10,type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function index(Request $request)
    {
        $boms = BomRepo::getList();
        return list_message("获取成功", true, $boms);
    }

    /**
     * @OA\Get(
     *     path="/boms/{id}",
     *     tags={"Bom"},
     *     summary="获取配件详情",
     *     description="获取配件详情",
     *     operationId="bom_show",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="配件ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function show(Request $request, $id)
    {
        try {
            $bom = BomRepo::fetch($id);
            return message("获取成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Post(
     *     path="/boms",
     *     tags={"Bom"},
     *     summary="添加配件",
     *     description="添加配件",
     *     operationId="bom_store",
     *     security={{"bearerAuth":{}}},
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"brand_id", "name", "specification", "model", "uom"},
     *              @OA\Property(property="brand_id", type="integer", example=0, description="品牌ID"),
     *              @OA\Property(property="name", type="string", example="", description="配件名称"),
     *              @OA\Property(property="specification", type="string", example="", description="规格"),
     *              @OA\Property(property="model", type="string", example="", description="型号"),
     *              @OA\Property(property="uom", type="string", example="", description="计量单位")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function store(Request $request)
    {
        try {
            $bom = BomRepo::store(0);
            return message("创建成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Put(
     *     path="/boms/{id}",
     *     tags={"Bom"},
     *     summary="更新配件",
     *     description="更新配件",
     *     operationId="bom_update",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="配件ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\RequestBody(
     *          required=true,
     *          @OA\JsonContent(
     *              type="object",
     *              required={"brand_id", "name", "specification", "model", "uom"},
     *              @OA\Property(property="brand_id", type="integer", example=0, description="品牌ID"),
     *              @OA\Property(property="name", type="string", example="", description="配件名称"),
     *              @OA\Property(property="specification", type="string", example="", description="规格"),
     *              @OA\Property(property="model", type="string", example="", description="型号"),
     *              @OA\Property(property="uom", type="string", example="", description="计量单位")
     *          )
     *     ),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function update(Request $request, $id)
    {
        try {
            $bom = BomRepo::store($id);
            return message("修改成功", true, $bom);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }

    /**
     * @OA\Delete(
     *     path="/boms/{id}",
     *     tags={"Bom"},
     *     summary="删除配件",
     *     description="删除配件",
     *     operationId="bom_destroy",
     *     security={{"bearerAuth":{}}},
     *     @OA\Parameter(name="id",in="path",description="配件ID",required=true,@OA\Schema(default="",type="integer")),
     *     @OA\Response(response=200, description="请求成功"),
     *     @OA\Response(response=401,description="鉴权失败"),
     *     @OA\Response(response=422, description="请求异常")
     * )
     */
    public function destroy(Request $request, $id)
    {
        try {
            BomRepo::destroy($id);
            return message("删除成功", true);
        } catch (\Exception $e) {
            return message($e->getMessage(), false);
        }
    }
}